
use bevy::{asset::AssetServer, color::{palettes::css::SEA_GREEN, Alpha, Color}, core::Name, prelude::{BuildChildren, Button, Changed, ChildBuild, ChildBuilder, Commands, ImageNode, Query, Res, With}, text::{TextColor, TextFont, TextSpan}, ui::{BackgroundColor, BorderColor, Interaction, Node, PositionType, UiRect, Val}, utils::default};

use crate::{components::Hud, input_map::Action};

pub fn setup_hud(mut commands: Commands){
    const ALPHA: f32 = 0.8;
    commands
        .spawn((
            Name::new("HUD"),
            Hud,
            Node {
                position_type: PositionType::Absolute,
                top: Val::Px(5.0),
                right: Val::Px(5.0),
                ..default()
            },
        ))
        .with_child((
            TextSpan::new("Level : "),
            TextFont::from_font_size(14.0),
            TextColor(SEA_GREEN.with_alpha(ALPHA).into()),
        ));
}


pub fn setup_button(mut commands: Commands,asset_server: Res<AssetServer>) {
    let button = |parent: &mut ChildBuilder,action: Action,img_path: &str| {
        parent.spawn((
            Name::new("Button"),
            action,
            Button,
            Node{
                width: Val::Px(64.0),
                height: Val::Px(64.0),
                justify_content: bevy::ui::JustifyContent::Center,
                align_items: bevy::ui::AlignItems::Center,
                border: UiRect::all(Val::Px(3.0)),
                ..default()
            },
            BorderColor(Color::NONE),
        ))
        .with_children(|parent|{
            parent.spawn((
                ImageNode::new(asset_server.load(img_path)),
                Node {
                    width: Val::Percent(90.0),
                    height: Val::Percent(90.0),
                    ..default()
                },
            ));
        });
    };
    commands
        .spawn((
            Node {
                position_type: PositionType::Absolute,
                right: Val::Px(20.0),
                bottom: Val::Px(20.0),
                ..default()
            },
            BackgroundColor(Color::NONE)
        ))
        .with_children(|parent|{
            button(parent,Action::ToggleInstantMove,"textures/instant_move_off.png");
            button(parent,Action::ToggleAutomaticSolution,"textures/automatic_solution.png");
            button(parent,Action::PreviousLevel,"textures/previous.png");
            button(parent,Action::NextLevel,"textures/next.png");
        });
}